<template>
  <header>
    <div class="banxin tanxin header_box">
      <section>欢迎来到叩丁狼积分商城</section>
      <ul>
        <li class="avater_li">
          <img :src="userInof.HeaderImg" alt="" />
          用户名：{{ userInof.nickName }}
        </li>
        <li>我的积分：{{ $store.state.myChicken }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li v-show="$store.state.isLogin" @click="userOut">退出</li>
        <li v-show="$store.state.isLogin" @click="unweixin">微信解绑</li>
        <li v-if="$store.state.isLogin" class="header_btn cart_btn">
          <img src="../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <b>{{ cartTotal }}</b>
        </li>
        <li
          class="header_btn login_btn"
          v-else-if="!$store.state.isLogin"
          @click="bdClick"
        >
          登录
        </li>
      </ul>
    </div>
  </header>
</template>

<script>
import {
  getUserInfoApi,
  WeixinLoginApi,
  UnbindWechatPhoneApi,
} from "@/request/api";
export default {
  data() {
    return {
      name: 1,
      userInof: {
        HeaderImg: require("../assets/images/userImg.png"),
        nickName: "游客",
        coin: "--",
      },
      cartTotal: 0,
    };
  },
  methods: {
    unweixin() {
      UnbindWechatPhoneApi().then((res) => {
        this.$store.dispatch("AsyncToast", {
          value: res.data.data,
          icon: "success",
        });
      });
    },
    bdClick() {
      this.$store.commit("changeIsShowLogin", true);
    },
    userOut() {
      localStorage.removeItem("x-auth-token");
      location.reload();
    },
    bingWatch() {
      // 如果地址栏 存在 微信的code  就去请求微信登录的接口
      // 如果存在code 才去请求 否则不做任何请求
      let code = this.$route.query.code;
      if (code) {
        WeixinLoginApi({ code }).then((res) => {
          if (res.data.code === 0) {
            this.$store.dispatch("AsyncToast", {
              value: "登录成功",
              icon: "success",
            });
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 更新
            this.$emit("updataKeyFn");
            this.$router.push(this.$route.path);
          } else if (res.data.code === 400) {
            this.$store.dispatch("AsyncToast", {
              value: "页面过期 请重新扫描二维码",
              icon: "danger",
            });
            this.$store.commit("changeIsShowLogin", true);
          } else if (res.data.code === 407) {
            this.$store.dispatch("AsyncToast", {
              value: "请使用手机号绑定微信后再使用",
              icon: "danger",
            });
            this.$store.commit("changeIsShowLogin", true);
            // 会过期，临时存储
            sessionStorage.setItem("uuid", res.data.uuid);
          } else {
            console.log(123123);
          }
        });
      }
    },
  },
  created() {
    getUserInfoApi().then((res) => {
      if (res.data.code === 0) {
        let s = res.data.data;
        this.$store.state.myChicken = this.userInof.coin = s.userInfo.coin;
        this.userInof.nickName = s.userInfo.nickName;
        this.userInof.HeaderImg = s.userInfo.headImg;
        this.cartTotal = s.cartTotal;
        this.$store.state.isLogin = true;
      }
    });
    this.bingWatch();
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";

header {
  background: @black;
  height: 40px;
  color: #fffefe;
  font-size: 14px;

  .header_box {
    align-items: center;
    height: 40px;

    ul {
      transform: translateX(20px);
      display: flex;
      height: 40px;
      align-items: center;

      li {
        margin-right: 20px;
        cursor: pointer;

        &.avater_li {
          display: flex;
          height: 26px;
          align-items: center;

          img {
            height: 26px;
            border-radius: 50%;
            margin-right: 8px;
          }
        }

        &.header_btn {
          width: 124px;
          height: 40px;
          background: @blue;
        }

        &.cart_btn {
          display: flex;
          align-items: center;
          justify-content: center;

          span {
            margin: 0 8px;
          }

          b {
            width: 22px;
            height: 22px;
            background: @orange;
            border-radius: 50%;
            text-align: center;
            line-height: 22px;
          }
        }

        &.login_btn {
          text-align: center;
          line-height: 40px;
        }
      }
    }
  }
}
</style>